package edu.java.homework.hw03.ipj07;

import java.util.Arrays;
import java.util.Scanner;

public class Exer04 {

    public static void main(String[] args) {

	int count = 0;
	Scanner input = new Scanner(System.in);
	System.out.println("Please, how many names you need: ");
	count = input.nextInt();
	if (count <= 0) {
	    throw new IllegalArgumentException(
		    "Error! The count of strings would be 1 or more");
	}
	
	String[] strArr=new String[count];
	//initialize the array of strings
	inputArrayOfStrings(strArr);
	//print the array
	System.out.println(Arrays.toString(strArr));
	
	sortInAscendingOrder(strArr);
	System.out.println();
	System.out.println(Arrays.toString(strArr));
	input.close();
    }
    
    public static void sortInAscendingOrder(String[] strArr){
	if (strArr == null) {
	    throw new IllegalArgumentException(
		    "Error! Null-pointed array of strings.");
	}
	// selection sort
	int length=strArr.length;
	int tempIndex=0;
	for(int i=0;i<=length-2;i++){
	   tempIndex=i;
	   for(int j=i+1; j<=length-1;j++){
	       int diff= strArr[tempIndex].compareTo(strArr[j]);
	       if(diff>0){
		   String tmpStr= strArr[tempIndex];
		   strArr[tempIndex]= strArr[j];
		   strArr[j]=tmpStr;
		   tempIndex=j;
	       }
	   }
	}
    }

    public static void inputArrayOfStrings(String[] strArr) {
	if (strArr == null) {
	    throw new IllegalArgumentException(
		    "Error! Null-pointed array of strings.");
	}

	Scanner input = new Scanner(System.in);
	int length = strArr.length;
	for (int i = 0; i < length; i++) {
	    System.out.print("name[" + i + "]= ");
	    String tmp = input.nextLine();
	    if (tmp.length() >= 10) {
		throw new IllegalArgumentException(
			"Error! The length of the string is over 9 symbols.");
	    }
	    strArr[i] = tmp;
	}

	input.close();
    }

}
